#!/usr/bin/env bash
# File: $SVNROOT/scripts/setup-android
# Note: Script to setup android to run bits and piece of Plexil

# Copyright (c) 2006-2015, Universities Space Research Association (USRA).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Universities Space Research Association nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY USRA ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL USRA BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#debug=
script_name=`basename $0`
android_bin_dir=/data/local
android_lib_dir=$android_bin_dir
android_plan_dir=$android_bin_dir
android_env_file=android.env
load_apk=1

function usage ()
{
cat <<EOF

Usage:      $script_name [options]

Options:    -v  := verbose ndk-build output
            -n  := don't install the apk file on the emulator

Examples:   $script_name

Notes:      

EOF
exit
}

function android-env ()
{
cat <<EOF
# Android Environment
export LD_LIBRARY_PATH=$android_bin_dir
#export PATH=\$PATH:.
run-plan()
{
  script=\$1.psx
  if [ ! -e \$1.psx ] ; then script=empty.psx ; fi
  echo "./TestExec -s \$script -p \$1.plx"
  ./TestExec -s \$script -p \$1.plx
}
EOF
} > $android_env_file

if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then
    usage
fi

until [ -z $1 ] ;
do
    if [ "$1" = "-v" ] ; then verbose=1 ; shift ;
    elif [ "$1" = "-n" ] ; then load_apk= ; shift ;
    elif [ "$1" = "-vn" ] || [ "$1" = "-nv" ] ; then verbose=1 ; load_apk= ; shift ;
    else echo " " ; echo "Unrecognized option: \"$1\"" ; usage ;
    fi
done

if [ ! -e "AndroidManifest.xml" ] ; then
    echo " "
    echo "Not in project directory (AndroidManifest.xml not found).  Giving up."
    usage
fi

# Just use ANDROID_SDK and ANDROID_NDK in the user's environemnt

if [ ! -z $ANDROID_NDK ] ; then
    echo " "
    echo "Running ndk-build..."
    echo " "
    ndk-build V=$verbose
fi

#if [ ! -z $ANDROID_SDK ] ; then
#    ant debug
#fi

echo " "
echo "checking for Android emulator..."
echo " "

# see if the emulator is running
devices=`adb devices`
sanity_check=`echo $devices | awk '{ print $1 }'`
emulator=`echo $devices | awk '{ print $5 }'`
status=`echo $devices | awk '{ print $6 }'`

if [ "$sanity_check" != "List" ] ; then
    echo "Android adb server was not running when $script_name was called.  Giving up."
    echo " "
    exit -1
elif [ -z $emulator ] ; then
    echo "Android emulator not shown in list of adb devices.  Giving up."
    echo " "
    exit -1
elif [ "$status" = "offline" ] ; then
    echo "$emulator appears to be $status.  Giving up."
    echo " "
    exit -1
else
    echo "Using $emulator"
fi

# copy the libraries and executables over

echo " "
echo "Copying libraries and executables to $emulator:$android_bin_dir..."
echo " "

for file in `ls libs/armeabi/*` ;
do
    echo "Copying $file"
    adb wait-for-device push $file $android_bin_dir 2> /dev/null ;
done

# copy the plans over

echo " "
echo "Copying Plexil plans and scripts to $emulator:$android_plan_dir..."
echo " "

for file in `ls ../plexil/examples/*.[pc][lsf][gx]` ;
do
    echo "Copying $file"
    adb push $file $android_plan_dir 2> /dev/null ;
done

# copy android.evn over
android-env
chmod +x $android_env_file
echo "Copying android.env to $emulator:$android_bin_dir..."
adb push $android_env_file $android_bin_dir 2> /dev/null
rm $android_env_file

if [ ! -z $ANDROID_SDK ] ; then
    echo " "
    if [ "$load_apk" = "1" ] ; then
        echo "Running ant install..."
        echo " "
        ant install
    else
        echo "Running ant debug..."
        echo " "
        ant debug
    fi
    echo " "
fi

# EOF
